const {
  toUrl,
  globalData
} = getApp();
import {
  request,
  baseUrl
} from "@/utils/request";
Page({
  toUrl,
  data: {
    form: {
      date: '',
      sex: '',
      value: '',
      nickname: '',
      interest_ids: '',
      head_pic: '',
    },
    active: 0,
    siery: [],
    userInfo: {},
    checkList: [],
    globalData,
    show: false,

  },
  confim() {
    this.show()
    console.log("弹窗消失")
    this.setData({
      'userInfo.interest_name': this.data.form.interest_name,
      'userInfo.interest_ids': this.data.form.interest_ids
    })

  },
  show() {
    this.setData({
      show: !this.data.show,
    });
  },
  //获取选中的兴趣数组
  getPush(e) {
    let {
      it,
      id,
      index
    } = e.currentTarget.dataset
    console.log(e.currentTarget.dataset);
    let list = this.data.siery
    if (this.data.siery[index].flag == false) {
      list[index].flag = this.data.siery[index].flag = true
    } else {
      list[index].flag = this.data.siery[index].flag = false
    }
    let xinList = list.filter((it) => {
      return it.flag == true
    })
    let idList = xinList.map((it) => {
      return it.id
    })
    let name = xinList.map((it) => {
      return it.name
    })
    // console.log(aa.);
    // console.log(xinList.join('      ,   '));
    this.setData({
      siery: list,
      "form.interest_ids": idList.join(','),
      "form.interest_name": name.join(','),
    })
  },
  onChooseAvatar(e) {
    console.log(11);
    wx.uploadFile({
      url: baseUrl + "currency/dyupload",
      filePath: e.detail.avatarUrl,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        Authorization: "Bearer " + getApp().globalData.token,
      },
      success: (res) => {
        console.log(JSON.parse(res.data));
        let head = JSON.parse(res.data)
        console.log(head);
        this.setData({
          "userInfo.head_pic": head.data.img,
          'form.head_pic': head.data.img
        })


        let data = {
          typeid: 1,
          head_pic: head.data.img,
        };

      },
    });
  },
  getBlur(e) {
    // console.log(e);
    let {
      typeid
    } = e.currentTarget.dataset;
    let {
      value
    } = e.detail;
    let data = {
      typeid: typeid,
    };

    switch (typeid) {
      case "2":
        data.nickname = value;
        //昵称
        break;
      case "5":
        //兴趣
        data.interest = value;
        break;
      default:
        break;
    }

    this.setData({
      'form.nickname': value
    })

  },
  userInfo() {
    console.log(99991111);
    request("user/getuserinfo").then((res) => {
      if (res.errcode == 1) {
        console.log(res.data);
        let userInfo = res.data;
        getApp().globalData.baseInfo = res.data
        let sex = userInfo.sex;
        let birthday = userInfo.birthday;
        console.log(sex);
        this.setData({
          userInfo,
          "form.sex": sex,
          "form.date": birthday,
        });
      }
    });
  },
  sexChange(e) {
    let {
      typeid
    } = e.currentTarget.dataset;
    console.log(e.currentTarget.dataset);
    let {
      detail
    } = e;
    let sex = "男";
    if (detail.value != 0) {
      sex = "女";
    }
    this.setData({
      form: {
        ...this.data.form,
        sex: sex,
        value: Number(Number(detail.value) + 1)
      },
    });
    let data = {
      typeid: typeid,
      sex: this.data.form.value,
    };

  },
  dateChange(e) {
    let {
      typeid
    } = e.currentTarget.dataset;
    let {
      detail
    } = e;

    this.setData({
      form: {
        ...this.data.form,
        date: detail.value
      },
    });

    let data = {
      typeid: typeid,
      birthday: this.data.form.date,
    };

  },
  loginOut() {
    // wx.clearStorage()
    wx.removeStorage({
      key: '_userInfo',
    });
    getApp().globalData.baseInfo = {}
    getApp().globalData.userInfo = null
    wx.showToast({
      title: '退出成功',
    })
    setTimeout(() => {
      wx.reLaunch({
        url: '/pages/home/home',
      })
    }, 2000);


  },
  onLoad(options) {
    let userInfo = getApp().globalData.baseInfo;
    console.log(userInfo);
    let sex = userInfo.sex;
    let birthday = userInfo.birthday;
    console.log(sex);
    this.setData({
      userInfo,
      "form.sex": sex,
      "form.date": birthday,
    });
    this.getXiQU()

  },
  getXiQU() {
    request("currency/getinterest").then((res) => {
      if (res.errcode == 1) {
        // console.log(res);
        let interest_ids = this.data.userInfo.interest_name
        interest_ids = interest_ids.split(',')
        console.log(interest_ids);
        if (interest_ids.length > 0) {
          res.data.map((it) => {
            it.flag = false
            interest_ids.map((its1) => {
              // console.log(it.name , its1,'-----');
              if (it.name.trim() == its1.trim()) {
                it.flag = true
              }
            })
          })
        } else {
          res.data.map((it) => {
            it.flag = false
          })
        }

        this.setData({
          siery: res.data
        })
      }
    });
  },

  commitSave() {
    wx.showLoading()
    let data = {
      typeid: 6,
      head_pic: this.data.form.head_pic,
      nickname: this.data.form.nickname,
      sex: this.data.form.value,
      birthday: this.data.form.date,
      interest_ids: this.data.form.interest_ids,
      interest: this.data.form.interest_name,
    }
    request("user/userhandle", {
      data
    }).then((res) => {
      if (res.errcode == 1) {
        wx.hideLoading()

        let perfect_integral = this.data.userInfo.perfect_integral


        if (res.data.is_integral == 1) {
          wx.showModal({
            title: '完善资料',
            content: `您已完善资料，${perfect_integral}积分已到账`,
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                console.log('确定');
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/pages/me/me',
                  })
                }, 1000);
              }
            }
          })
        } else {
          wx.showToast({
            title: '保存成功',
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/me/me',
            })
          }, 2000);
        }
      }
    });
  },

  onReady() {},

  onShow() {},

  onHide() {},

  onUnload() {
    console.log(111);
  },

  onPullDownRefresh() {},

  onReachBottom() {},

  onShareAppMessage() {},
});